using System.Collections.Generic;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using HCD.Entities;
using Abp.Application.Services;
using HCD.His.Dtos;
using Abp.Collections.Extensions;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace HCD.His
{
    /// <summary>
    /// 应用层服务的接口实现方法
    ///</summary>
    [RemoteService(false)]
    public class HisAppService : EnterpriseAppServiceBase, IHisAppService
    {
        public IRepository<HisOrders, long> HisOrdersRepository { get; set; }
        public IRepository<HisOperation, long> HisOperationRepository { get; set; }
        public IRepository<HisExam, long> HisExamRepository { get; set; }
        public IRepository<HisExamReport, long> HisExamReportRepository { get; set; }
        public IRepository<HisLabTest, long> HisLabTestRepository { get; set; }
        public IRepository<HisLabResult, long> HisLabResultRepository { get; set; }

        /// <summary>
        /// 获取病人医嘱
        /// </summary>
        /// <returns></returns>
        public async Task<List<HisOrdersDto>> GetPatHisOrdersDto(HisOrdersGetInput input)
        {
            var list = HisOrdersRepository.GetAll().Where(w => w.PatientId == input.PatientId && w.VisitId == input.VisitId);

            return ObjectMapper.Map<List<HisOrdersDto>>(await list.ToListAsync());
        }

        /// <summary>
        /// 获取病人手术
        /// </summary>
        /// <returns></returns>
        public async Task<List<HisOperationDto>> GetPatHisOperationDto(HisOperationGetInput input)
        {
            var list = HisOperationRepository.GetAll().Where(w => w.PatientId == input.PatientId && w.VisitId == input.VisitId);

            return ObjectMapper.Map<List<HisOperationDto>>(await list.ToListAsync());
        }

        /// <summary>
        /// 获取病人检查申请单
        /// </summary>
        /// <returns></returns>
        public async Task<List<HisExamDto>> GetPatHisExamDto(HisExamGetInput input)
        {
            var exam = from ex in HisExamRepository.GetAll()
                       join re in HisExamReportRepository.GetAll() on ex.ExamNo equals re.ExamNo
                       where ex.PatientId == input.PatientId && ex.VisitId == input.VisitId
                       select new
                       {
                           ex,
                           re
                       };
            return (await exam.GroupBy(g => g.ex, m => m.re).ToListAsync()).Select(s =>
            {
                HisExamDto hisExam = new HisExamDto();
                hisExam = ObjectMapper.Map<HisExamDto>(s.Key);
                hisExam.lstReport = ObjectMapper.Map<List<HisExamReportDto>>(s.ToList());
                return hisExam;
            }).ToList();
        }


        /// <summary>
        /// 获取病人检查结果
        /// </summary>
        /// <returns></returns>
        public async Task<List<HisExamReportDto>> GetPatHisExamReportDto(HisExamReportGetInput input)
        {
            var list = HisExamReportRepository.GetAll();
            if (!string.IsNullOrEmpty(input.ExamNo))
                list = list.Where(w => w.ExamNo == input.ExamNo);
            if (!string.IsNullOrEmpty(input.ReportNo))
                list = list.Where(w => w.ReportNo == input.ReportNo);

            return ObjectMapper.Map<List<HisExamReportDto>>(await list.ToListAsync());
        }


        /// <summary>
        /// 获取病人检验申请单
        /// </summary>
        /// <returns></returns>
        public async Task<List<HisLabTestDto>> GetPatHisLabTestDto(HisLabTestGetInput input)
        {
            var lab = from la in HisLabTestRepository.GetAll()
                      join re in HisLabResultRepository.GetAll() on la.LabTestNo equals re.LabTestNo
                      where la.PatientId == input.PatientId && la.VisitId == input.VisitId
                      select new
                      {
                          la,
                          re
                      };

            return (await lab.GroupBy(g => g.la, m => m.re).ToListAsync()).Select(s =>
            {
                HisLabTestDto labDto = new HisLabTestDto();
                labDto = ObjectMapper.Map<HisLabTestDto>(s.Key);
                labDto.lstLabResult = ObjectMapper.Map<List<HisLabResultDto>>(s.ToList());
                return labDto;
            }).ToList();
        }


        /// <summary>
        /// 获取病人检查结果
        /// </summary>
        /// <returns></returns>
        public async Task<List<HisLabResultDto>> GetPatHisLabResultDto(HisLabResultGetInput input)
        {
            var list = HisLabResultRepository.GetAll();
            if (!string.IsNullOrEmpty(input.LabTestNo))
                list = list.Where(w => w.LabTestNo == input.LabTestNo);

            return ObjectMapper.Map<List<HisLabResultDto>>(await list.ToListAsync());
        }
    }
}


